﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Casco.Erz.DAL;
using Casco.Erz.Models;
using PagedList;
using System.Xml.Linq;

namespace Casco.Erz.Controllers
{
    public class HomeController : Controller
    {
        
        // GET: /Home/跳转到首页 http://casco.com.cn
        private Position_dbContext db = new Position_dbContext(); 
        public ActionResult Index()
        {
            return View();
        }
        /*获取职位列表*/
        public ActionResult GetPositionList(string posType,string searchString,string currentFilter,int? page) 
        {   
            /*热门职位[可以配置，从XML中读取]*/
            XElement root = XElement.Load(Server.MapPath("/Content/xml/HotPositions.xml"));
            var hotPositions = from el in root.Elements("Position")
                               select new Position
                               {
                                   PositionID = (int)el.Element("Id"),
                                   Header = (string)el.Element("Name")
                               };
            ViewBag.HotPos = hotPositions;
            /*职位搜索From Sap RFC*/
            Position_sapContext sapDb = new Position_sapContext();
            List<Position> sapPosList = sapDb.Positions;
            /*职位搜索*/
            var posList = from s in sapPosList
                          group s by s.GzPlace into g
                          select new PositionPlace { 
                            Pname = g.Key
                          };
            ViewBag.PositionItems = posList.ToList();

            if(searchString != null){
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.PosType = posType;
            ViewBag.CurrentFilter = searchString;

            var positions = from s in sapPosList
                            select s;
            if(!string.IsNullOrEmpty(searchString)){
                positions = positions.Where(s => s.Header.Contains(searchString));
            }
            if (posType != null)
            {
                positions = positions.Where(s => s.GzPlace == posType.Trim());
            }

            int pageSize = 8;
            int pageNum = (page ?? 1);
            return View(positions.OrderByDescending(s =>s.BegDate).ToPagedList(pageNum,pageSize));
        }
        /*了解卡斯柯*/
        public RedirectResult About()
        {
           return Redirect("http://casco.com.cn/about/default.aspx?t=1&c=1");
        }
        /*获取某个职位信息*/
        public ActionResult GetOnePosition(string postId)
        {
            Position_sapContext sapDb = new Position_sapContext();
            List<Position> sapPosList = sapDb.Positions;
            Position PostItem = (from s in sapPosList
                                 where s.PositionID == Convert.ToInt32(postId)
                                 select s).SingleOrDefault();
            //if (PostItem ==null)
            //{
            //    Response.StatusCode = 404;
            //    return View();
            //}
            /*热门职位[可以配置，从XML中读取]*/
            XElement root = XElement.Load(Server.MapPath("/Content/xml/HotPositions.xml"));
            var hotPositions = from el in root.Elements("Position")
                               select new Position
                               {
                                   PositionID = (int)el.Element("Id"),
                                   Header = (string)el.Element("Name")
                               };
            ViewBag.HotPos = hotPositions;
            return View(PostItem);
        }
        /*招聘帮助*/
        public ActionResult Help() {
            /*热门职位[可以配置，从XML中读取]*/
            XElement root = XElement.Load(Server.MapPath("/Content/xml/HotPositions.xml"));
            var hotPositions = from el in root.Elements("Position")
                               select new Position
                               {
                                   PositionID = (int)el.Element("Id"),
                                   Header = (string)el.Element("Name")
                               };
            ViewBag.HotPos = hotPositions;
            return View();
        }
    }
}
